﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace YummyGame.Framework
{
    public class Skill : IPoolObject
    {
        public SkillOwner Owner;
        public SkillPrototype Prototype;
        public SkillBaseInfo BaseInfo;
        public SkillDescriptor descriptor;
        public Dictionary<string, SkillActionPrototype> m_actions;
        public Dictionary<string, float> m_variables;

        public ExpressionContext m_context;
        public Dictionary<string, Expression> m_expressions;

        private List<SkillExecuter> m_executers = new List<SkillExecuter>();

        private float m_cd;

        public delegate string SkillLoader(string path);
        public static SkillLoader Loader = (path) =>
        {
            TextAsset ta = Resources.Load<TextAsset>(path);
            return ta.text;
        };
        

        public Skill()
        {
            m_variables = new Dictionary<string, float>();
            m_expressions = new Dictionary<string, Expression>();
        }

        public void Use()
        {
            IPool<SkillExecuter> pool = SingletonPool<SkillExecuter>.Instance.Create(() => Activator.CreateInstance<SkillExecuter>());
            SkillExecuter excuter = pool.Get();
            excuter.Prototype = Prototype;
            excuter.Owner = Owner;
            excuter.Runtime = this;
            ProtoActionToRuntime(excuter);

            m_executers.Add(excuter);
        }

        private void ProtoActionToRuntime(SkillExecuter executer)
        {
            foreach (var name in m_actions.Keys)
            {
                executer.actions.Add(name, m_actions[name].ToRuntime(executer));
            }
        }

        public bool CanUse()
        {
            if (m_cd > 0) return false;
            return true;
        }

        public void Tick()
        {
            for (int i = 0; i < m_executers.Count; i++)
            {
                if (!m_executers[i].Tick())
                {
                    m_executers[i].Dispose();
                    m_executers.RemoveAt(i);
                    i--;
                }
            }
            m_cd--;
        }

        public void OnDisable()
        {
            m_variables.Clear();
            m_expressions.Clear();
        }

        public void OnEnable()
        {
            OnDisable();
        }

        public void DefineVariable(string key, float value)
        {
            if (!HasVariable(key)) m_variables[key] = value;
            else throw new Exception("变量被重复定义：" + key);
        }

        public void SetVariable(string key, float value)
        {
            if (!HasVariable(key)) throw new Exception("没有定义这个变量：" + key);
            m_variables[key] = value;
            Owner.OnVariableChanged(key, value);
        }

        public float GetVariable(string key)
        {
            if (m_variables.ContainsKey(key)) return m_variables[key];
            return 0;
        }

        public bool HasVariable(string key)
        {
            return m_variables.ContainsKey(key);
        }

        public Expression GetExpression(string key)
        {
            Expression res = null;
            if (m_expressions.TryGetValue(key, out res)) return res;
            return null;
        }
    }

    public class SkillConfig
    {
        public Type OwnerType;
        public float TickCount;

    }

    
}
